//
//  ItemOfLvXingDi.m
//  SeeTheWorld
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ItemOfLvXingDi.h"
#import "PCH.pch"
#import "LvXingDi.h"
#import "ModelOfLvXingDi.h"
#import "VCOfClickLvXingDi.h"
#import <UIImageView+WebCache.h>


@interface ItemOfLvXingDi () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;

@property (nonatomic, retain)NSMutableArray *arrayOfModel;

@end

@implementation ItemOfLvXingDi

- (void)dealloc {
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
    [_arrayOfModel release];
    [_layout release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.arrayOfModel = [NSMutableArray array];
        
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.minimumInteritemSpacing = 20;
        self.layout.minimumLineSpacing = 20;
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        self.layout.itemSize = CGSizeMake((self.contentView.frame.size.width - 60) / 2.0, (self.contentView.frame.size.width - 60) / 2);
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.layout];
        [self.contentView addSubview:self.collectionView];
        [_collectionView release];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        [self.collectionView registerClass:[LvXingDi class] forCellWithReuseIdentifier:@"pool"];
        
        
    }
    
    return self;
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrayOfModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LvXingDi *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    [cell passModel:[self.arrayOfModel objectAtIndex:indexPath.item]];
    
    return cell;
    
}

- (void)passModel:(NSMutableArray *)arrayOfModel {
   
    self.arrayOfModel = arrayOfModel;
    
    [self.collectionView reloadData];
    
}

#pragma mark - 布局子视图
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    
    self.collectionView.frame = self.contentView.frame;
    

}

/** 点击Item, 协议传值 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ModelOfLvXingDi *model = [self.arrayOfModel objectAtIndex:indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(passID:)]) {
        
        [self.delegate passID:model.id];
    }
    
}


















@end
